const express = require('express');
const bodyParser = require('body-parser');

const app = express();
app.use(bodyParser.json());
app.use((req, res, next) => {
    res.header('Access-Control-Allow-Origin', '*'); // 允许所有域名访问
    res.header('Access-Control-Allow-Headers', 'Content-Type');
    next();
});

let data = [
    'dsa'
]
// 获取关键词列表
app.get('/api/keywords', (req, res) => {
    try {
        res.json({keywords: data});
    } catch (error) {
        res.status(500).json({error: error.message});
    }
});
// 获取关键词列表
app.get('/api/clear', (req, res) => {
    try {
        data = []
        res.json({success: true});
    } catch (error) {
        res.status(500).json({error: error.message});
    }
});
// 添加关键词
app.post('/api/keywords', (req, res) => {
    const {keyword} = req.body;
    if (!keyword) {
        return res.status(400).json({error: '关键词不能为空'});
    }

    try {
        data.push(keyword)
        res.json({success: true});
    } catch (error) {
        res.status(500).json({error: error.message});
    }
});

// 启动服务器
const PORT = 3000;
app.listen(PORT, () => {
    console.log(`Server is running on http://localhost:${PORT}`);
});
